package edu.rit.hvz.android;

import edu.rit.hvz.android.DataLayer.Player;
import edu.rit.hvz.android.DataLayer.PlayerDBAdapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class PlayerProfile extends ListActivity {
	private Player player;
	private PlayerDBAdapter db;
	private SimpleCursorAdapter adapter;
	
	//Profile specific fields
	private TextView name;
	private TextView tags;
	private TextView team;
	private TextView clan;
	
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);

		Utils.onActivityCreateSetTheme(this);

		db = new PlayerDBAdapter(this);
        setContentView(R.layout.player_profile_layout);
        
        name = (TextView)findViewById(R.id.player_name);
        tags = (TextView)findViewById(R.id.player_tags);
        team = (TextView)findViewById(R.id.player_team);
        clan = (TextView)findViewById(R.id.player_clan);
        
        if(this.getIntent().getExtras() != null) {
        	db.open();
        	player =  db.getPlayer(getIntent().getLongExtra(Player.BUNDLE_KEY, 0));
        	if( player.getPlayerName() != null )
        		name.setText(player.getPlayerName());
        	tags.setText(Integer.toString(player.getTags()));
        	if( player.getTeam() != null)
        		team.setText(player.getTeam());
        	if( player.getClan() != null)
        		clan.setText(player.getClan());
        	//clan.setText()    Support needs to be added for this in the Player class ( and DB )
        	db.close();
        	
        	if( player.getTaggedPlayers().getCount() > 0){
        	startManagingCursor(player.getTaggedPlayers());
        	
        	adapter = new SimpleCursorAdapter(
                    this, // Context.
                    R.layout.player_list_item,  // Specify the row template to use (here, two columns bound to the two retrieved cursor rows).
                    player.getTaggedPlayers(),                                              // Pass in the cursor to bind to.
                    new String[] {PlayerDBAdapter.NAME, PlayerDBAdapter.TEAM, PlayerDBAdapter.CLAN},           // Array of cursor columns to bind to.
                    new int[] {R.id.name, R.id.team, R.id.clan});  // Parallel array of which template objects to bind to those columns.
        	setListAdapter(adapter);
        	}

        }
    }
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id){
		this.getIntent().putExtra(Player.BUNDLE_KEY, id);
		startActivity(getIntent());
		finish();
		
	}
}
